package top.dingyingying.springboot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.dingyingying.springboot3.base.PageBean;
import top.dingyingying.springboot3.base.R;
import top.dingyingying.springboot3.dao.TbClassMapper;
import top.dingyingying.springboot3.entity.TbClass;
import top.dingyingying.springboot3.service.TbClassService;
import top.dingyingying.springboot3.vo.PageVo;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbClassServiceImpl implements TbClassService {
    private final TbClassMapper tbClassMapper;

    private TbClass queryCname(String cname) {
        QueryWrapper<TbClass> wrapper = new QueryWrapper<>();
        wrapper.eq("cname", cname);
//        查询单一记录的方法
        TbClass tbClass = tbClassMapper.selectOne(wrapper);
        return tbClass;
    }

    @Override
    public R<String> insert(TbClass tbClass) {
//        先校验名称是否存在
        TbClass check = queryCname(tbClass.getCname());
        if (check != null){
            return R.fail("班级已经存在", null);
        }
        int i = tbClassMapper.insert(tbClass);
        return i == 1 ? R.ok("新增成功", null) : R.fail("新增失败", null);
    }


    @Override
    public R<List<TbClass>> query(TbClass tbClass) {
//        动态sql（查询条件）
        QueryWrapper<TbClass> wrapper = new QueryWrapper<>();
//        如果cname有传入，就添加过滤
//        StringUtils.hasText表示字符串不能为null，或者全是空白字符
        wrapper.like(StringUtils.hasText(tbClass.getCname()), "cname", tbClass.getCname());
        wrapper.like(StringUtils.hasText(tbClass.getDescription()), "description", tbClass.getDescription());
        List<TbClass> list = tbClassMapper.selectList(wrapper);
        return R.ok(list);
    }

    @Override
    public R<PageVo<TbClass>> queryPage(PageBean pageBean, TbClass tbClass) {
//        分页的参数
        IPage<TbClass> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
//        动态sql（查询条件）
        QueryWrapper<TbClass> wrapper = new QueryWrapper<>();
//        添加排序(desc是逆序，asc是正序)
        wrapper.orderByDesc("create_at");
//        如果cname有传入，就添加过滤
//        StringUtils.hasText表示字符串不能为null，或者全是空白字符
        wrapper.like(StringUtils.hasText(tbClass.getCname()), "cname", tbClass.getCname());
        wrapper.like(StringUtils.hasText(tbClass.getDescription()), "description", tbClass.getDescription());
//      分页查询的结果是page的对象
     page = tbClassMapper.selectPage(page,wrapper);
        return R.ok(page);
    }

    @Override
    public R<TbClass> queryById(Integer id) {
        TbClass tbClass = tbClassMapper.selectById(id);
        return tbClass == null ? R.fail("查询失败", null) : R.ok(tbClass);

    }

    @Override
    public R<String> update(TbClass tbClass) {
//        校验名称是否存在
        TbClass check = queryCname(tbClass.getCname());
        if (check != null && !check.getId().equals(tbClass.getId())){
            return R.fail("班级已经存在", null);
        }
        int i = tbClassMapper.updateById(tbClass);
        return i == 1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }

    @Override
    public R<String> delete(Integer id) {
        int i = tbClassMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }
}
